﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZSZ.CommonMVC;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;
using ZSZ.Website.Areas.WebAdmin.Filters;
using ZSZ.Website.Areas.WebAdmin.Models;

namespace ZSZ.Website.Areas.WebAdmin.Controllers
{
    [AdminAuthorize(IsCheck = true)]
    public class RoleController : Controller
    {
        public IRoleService roleService { get; set; }
        public IPermissionService permissionService { get; set; }
        public IRolePermissionService rolePermissionService { get; set; }
        // GET: WebAdmin/Role
        public ActionResult List(int pageIndex = 1)
        {
            long total;
            int pageSize = 10, maxPagerCount = 10;
            List<Role> list = roleService.GetPagedData(out total, pageIndex, pageSize).ToList();
            ViewBag.totalCount = total;
            ViewBag.pageIndex = pageIndex;
            ViewBag.pageSize = pageSize;
            ViewBag.maxPagerCount = maxPagerCount;
            return View(list);
        }

        [HttpGet]
        public ActionResult Add()
        {
            PermissionDTO[] perms = permissionService.GetPermissions();//所有可用的权限项
            return View(perms);
        }

        [HttpPost]
        public ActionResult Add(RoleAddViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = MVCHelper.GetValidMsg(ModelState)
                });
            }
            //新增角色
            long roleId = roleService.Add(model.name, model.description);
            //新增权限
            rolePermissionService.Add(roleId, model.permissions);
            return Json(new AjaxResult { Status = "ok" });
            //return View();
        }

        [HttpGet]
        public ActionResult Edit(long id)
        {
            RoleDTO role = roleService.GetDTOById(id);
            if (role == null)
            {
                return View("Error", (object)"获取不到角色信息");
            }
            PermissionDTO[] AllPerms = permissionService.GetPermissions();
            RoleEditGetViewModel model = new RoleEditGetViewModel();
            model.Role = role;
            model.AllPerms = AllPerms;
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(RoleEditViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult
                {
                    Status = "error",
                    ErrorMsg = MVCHelper.GetValidMsg(ModelState)
                });
            }
            RoleDTO role = roleService.GetDTOById(model.id);
            if (role == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "角色信息不存在" });
            }
            //更新角色
            roleService.Update(model.id, model.name, model.description);
            //更新权限
            rolePermissionService.Update(role.Id, model.permissions);
            return Json(new AjaxResult { Status = "ok" });
        }


        [HttpPost]
        public ActionResult Delete(long id)
        {
            RoleDTO role = roleService.GetDTOById(id);
            if (role == null)
            {
                return View("Error", (object)"获取不到角色信息");
            }
            roleService.MarkDeleted(id);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        public ActionResult BatchDelete(long[] selectdIds)
        {
            List<long> list = new List<long>();
            foreach (long id in selectdIds)
            {
                list.Add(id);
            }
            roleService.BatchMarkDeleted(list);
            return Json(new AjaxResult { Status = "ok" });
        }
    }
}